home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / SORTING.SWG / 0030_SORTFAST.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  3KB  |  65 lines

  1. {
  2. > I might share With you a sorting Procedure which I developed For
  3. > 'those Arrays we were talking about:
  4. > ...
  5. > Exeperimentally I used it on 1485 Strings, which took about 3 sec
  6. > on my 386DX40.  Could you advise on some method to do it even
  7. > faster?
  8.  
  9. I'll share With you a little sort routine which I use often in my Programs
  10. whenever I need a fast and efficient routine With very low overhead... It Uses
  11. considerably less code than your example, and should outperForm it. (It would
  12. be even faster if it was all coded in Assembly!-- hint hint DJ) :-)
  13. }
  14.  
  15. Procedure Sort_It( totalItems : Word );
  16.  
  17.   Function Is_Less( TemPtr1, TemPtr2 : Pointer ) : Boolean;
  18.   begin
  19.     Is_Less := ( YourStruct(TemPtr1^).Item < YourStruct(TemPtr2^).Item );
  20.   end;
  21.  
  22. Var
  23.   I,J : Word;
  24.   Cur : Word;
  25.  
  26. begin
  27.   For I := 1 to Pred(totalItems) do
  28.   begin
  29.     Cur := I;
  30.  
  31.     For J := I + 1 to totalItems do
  32.       if Is_Less( Item[J], Item[Cur] ) then
  33.         ExchangeLongInts( LongInt(Item[J]), LongInt(Item[Cur]) );
  34.   end; { For }
  35.  
  36. end; { Proc }
  37.  
  38. {
  39. There's a couple things I should explain: The "ExchangeLongInts" Procedure is
  40. from the TurboPower Opro's OpInline Unit. All it does is exchange two LongInt
  41. Types without you having to use a temporary Variable. It's fast and convenient,
  42. but not the only possible solution here. (I'm Typecasting the Pointer into a
  43. LongInt For a 4-Byte swap.)
  44.  
  45. "totalItems" is the total number of items in your Array to sort.
  46.  
  47. "Item" is the actual Array; Item : Array[1..xx] of Pointer_to_Record;
  48.  
  49. "YourStruct" used in the "Is_Less" Function is Typecasting the actual structure
  50. or Record that "Item" is referring to. It's the only portion of the code which
  51. looks at your actual data. to reverse the sort order, simply change the "<" to
  52. ">". to change what is being sorted, just change the ".Item" to something else
  53. like ".Name" or ".Zip" or whatever else might be contained in your structure.
  54.  
  55. This routine is simple, has a minimum amount of code, Uses very little stack,
  56. works only With Pointers and you are only sorting memory addresses; it never
  57. actually move any of your physical data. (if you did, then it would be slow.)
  58.  
  59. It'll sort several thousand items in only a couple seconds even on slower
  60. machines, and is super on small volume runs. I would imagine that it would
  61. (90 min left), (H)elp, More? start loosing steam around 1,000 to 2,000 items, but For me, it's the best
  62. choice when memory is at a premium and the Arrays are fairly small.
  63. }
  64.  
  65.